package com.loadburn.heron.render;

import com.google.common.collect.Maps;
import com.google.inject.Provider;

import java.io.Serializable;
import java.util.Map;

/**
 * @author slacrey (scstlinfeng@yahoo.com)
 *         Date: 14-1-11
 */
public class AdditionAttributesProvider implements Provider<AdditionAttributes>, Serializable{


    private static final long serialVersionUID = -4848922067440034042L;

    @Override
    public AdditionAttributes get() {

        final Map<String, Object> attributeMap = Maps.newLinkedHashMap();

        return new AdditionAttributes() {
            @Override
            public Map<String, Object> asMap() {
                return attributeMap;
            }

            public AdditionAttributes addAttribute(String attributeName, Object attributeValue) {
                attributeMap.put(attributeName, attributeValue);
                return this;
            }

            public AdditionAttributes addAllAttributes(Map<String, ?> attributes) {
                if (attributes != null) {
                    attributeMap.putAll(attributes);
                }
                return this;
            }

            public AdditionAttributes mergeAttributes(Map<String, ?> attributes) {
                if (attributes != null) {
                    for (String key : attributes.keySet()) {
                        if (!attributeMap.containsKey(key)) {
                            attributeMap.put(key, attributes.get(key));
                        }
                    }
                }
                return this;
            }

            public boolean containsAttribute(String attributeName) {
                return attributeMap.containsKey(attributeName);
            }
        };
    }
}
